﻿Imports System
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Imports OMANE.Modules.Indicateurs.Services

Namespace Views


    Partial Public Class ViewDetailEdit

        Private hierarchydata As New ObjectModel.ObservableCollection(Of Object)

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub New()
            MyBase.New()

            Me.InitializeComponent()

            ' Insert code required on object creation below this point.



            hierarchyLst.ItemsSource = hierarchydata
        End Sub

        Private Sub EditPropertyButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
            If hierarchydata.Count = 0 Then hierarchydata.Add(New With {.Name = "[Current]", .Target = DataContext})

            Dim bt As Button = sender
            Dim o = TryCast(bt.DataContext, VMEditableProperty)
            If o IsNot Nothing Then
                DataContext = New VMEditableObject(o.Value)
                hierarchydata.Add(New With {.Name = "[" & o.Name & "]", .Target = DataContext})
            Else
                Dim n = bt.DataContext.GetType().Name
                DataContext = New VMEditableObject(bt.DataContext)
                hierarchydata.Add(New With {.Name = "[" & n & "]", .Target = DataContext})
            End If


        End Sub

        Public Sub hierarchySelected(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)

        End Sub

        Private Sub TextBlock_MouseLeftButtonDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
            Dim o = CType(sender, FrameworkElement).DataContext

            Dim opos = hierarchydata.IndexOf(o)

            If opos = hierarchydata.Count - 1 Then Return

            While opos <> hierarchydata.Count - 1
                hierarchydata.RemoveAt(hierarchydata.Count - 1)
            End While

            If DataContext IsNot o.Target Then DataContext = o.Target
        End Sub



    End Class


    Namespace Selectors
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <remarks></remarks>
        Friend Class PropertyEditorTemplateSelector
            Inherits DataTemplateSelector

            ''' <summary>
            ''' 
            ''' </summary>
            ''' <remarks></remarks>
            Public Overrides Function SelectTemplate(ByVal item As Object, ByVal container As System.Windows.DependencyObject) As System.Windows.DataTemplate
                Dim cpres = TryCast(container, ContentPresenter)
                If cpres Is Nothing Then Return Nothing
                Dim cctrl = TryCast(cpres.TemplatedParent, ContentControl)
                If cctrl Is Nothing Then Return Nothing

                Dim prop = TryCast(item, VMEditableProperty)
                If prop IsNot Nothing Then
                    Dim dt = TryCast(prop.EditorTemplate, DataTemplate)
                    If dt IsNot Nothing Then
                        Return dt
                    End If
                    Dim dtstr = TryCast(prop.EditorTemplate, String)
                    If String.IsNullOrEmpty(dtstr) Or String.IsNullOrWhiteSpace(dtstr) Then
                        If prop.Type.IsEnum Then
                            dt = TryCast(cctrl.Resources("enum"), DataTemplate)
                        ElseIf prop.Type.IsSubclassOf(GetType(Windows.Media.Brush)) Then
                            dt = TryCast(cctrl.Resources("brush"), DataTemplate)
                        ElseIf prop.Type.GetInterfaces().Contains(GetType(IEnumerable)) And prop.Type IsNot GetType(String) Then
                            dt = TryCast(cctrl.Resources("collection"), DataTemplate)
                        Else
                            dt = TryCast(cctrl.Resources(prop.Type.Name.ToLower()), DataTemplate)
                        End If
                        Return dt
                    End If
                    dt = TryCast(Application.Current.Resources(dtstr), DataTemplate)
                    Return dt
                End If
                Return MyBase.SelectTemplate(item, container)
            End Function
        End Class

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <remarks></remarks>
        Friend Class ChartZoneTemplateSelector
            Inherits DataTemplateSelector

            ''' <summary>
            ''' 
            ''' </summary>
            ''' <remarks></remarks>
            Public Overrides Function SelectTemplate(ByVal item As Object, ByVal container As System.Windows.DependencyObject) As System.Windows.DataTemplate
                Dim c = TryCast(item, VMZoneGraphDefinition)
                If c Is Nothing Then Return Nothing
                Dim c2 = TryCast(c.DetailInstance, VMChartZone)
                If c2 Is Nothing Then Return Nothing

                Dim f = TryCast(container, FrameworkElement)
                If f Is Nothing Then Return Nothing
                'If f.TemplatedParent is Nothing then Return not
                Return f.Resources("vmchartzone")
            End Function
        End Class
    End Namespace


    Public Class AnonymousDatasourceConverter
        Implements IValueConverter


        Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
            Return Nothing
        End Function

        Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
            If ModuleIndicateurs.AppInstance Is Nothing Then Return Nothing
            Dim iser = ModuleIndicateurs.AppInstance.GetService(Of IDataProviderService)()
            If iser Is Nothing Then Return Nothing
            Dim ser = TryCast(iser, DataProviderService)
            If ser Is Nothing Then Return Nothing

            Dim prov = TryCast(value.Provider, IDataProvider)


        End Function
    End Class
End Namespace